local CommonOHOH = _G.CommonOHOH;

local M = CommonOHOH.class.new(script, CommonOHOH.files.BaseComponent());

local DetailsClass=require(script.Parent.Parent.Details)

local CallBackManager = _G.ClientOHOH.load.CallBackManager();
local UIManager = _G.ClientOHOH.load.UIManager();

function M.new(...)
	return M(...);
end;


function M.ctor(self,parent)
    M.super.ctor(self,parent);

    self.DetailsGui=DetailsClass.new(self.parent:GetDetailsGui())
    self.parent.ActiveBroEventEvent:Connect(function(data)
        self:Full(data)

    end)

    self.ui=self.DetailsGui:GetUiInsts()

    assert(self.ui,"self.ui==nil")
    self.EquipEvent= self.parent.EquipEvent
    self.UnEquipEvent= self.parent.UnEquipEvent
    self.UpgradeEvent= self.parent.UpgradeEvent
    self.SellEvent= self.parent.SellEvent


    CallBackManager:AddRbxConnect(self,self.ui.equip_btn.Activated, function()
        if not self:isInCd() then
            return 
        end
        if not self.Data then
            return 
        end
        if self.Data.isEquip then
            self.UnEquipEvent:Action(self:GetEventData())
        else
            self.EquipEvent:Action(self:GetEventData())
        end
    end);

    CallBackManager:AddRbxConnect(self,self.ui.upgrade_btn.Activated, function()
        if not self:isInCd() then
            return 
        end
        if not self.Data then
            return 
        end
        self.UpgradeEvent:Action(self:GetEventData())
    end);


    CallBackManager:AddRbxConnect(self,self.ui.sell_btn.Activated, function()
        if not self:isInCd() then
            return 
        end
        if not self.Data then
            return 
        end
        self.SellEvent:Action(self:GetEventData())
    end);

    CallBackManager:AddRbxConnect(self,self.ui.weapon_btn.Activated, function()
        if not self:isInCd() then
            return 
        end
        if not self.Data then
            return 
        end
         ---
         
        local gui= _G.ClientOHOH.load.UIManager():GetGUI("WeaopnEquipGui")
        gui:Show(self.Data.userId,self.Data.id)

        local changeCon=nil
        changeCon=self.parent.ActiveBroEventEvent:Connect(function(data)
            gui:changeBroId(self.Data.userId,self.Data.id)
        end)

        local hideCon=nil
        hideCon=gui.BaseHideEvent:Connect(function()
            if changeCon then
                changeCon:Disconnect()
                changeCon=nil
            end
            if hideCon then
                hideCon:Disconnect()
                hideCon=nil
            end
        end)
        
    end);

    CallBackManager:AddRbxConnect(self,self.ui.invite_btn.Activated, function()
        if not self:isInCd() then
            return 
        end
        if not self.Data then
            return 
        end
        self:InvitedFriend()
    end);

end;

function M.dtor(self)
	M.super.dtor(self);
end;

function M.Full(self,data)
    if self.ConList then
        for _, con in ipairs( self.ConList) do
            con:Disconnect()
        end
    end
    self.ConList={}
    self.Data=data

   local con1=self.Data.DtorEvent:Connect(function()
        self.Data=nil
        self.DetailsGui:Clear()
    end)

    table.insert(self.ConList,con1)
    local con2=self.Data.UpdateEvent:Connect(function()
        self.DetailsGui:FullUi(data.isEquip,data.upgrade,data.sell,data.level,data.name,data.wpn,data.atk)
    end)
    table.insert(self.ConList,con2)

    self.DetailsGui:FullUi(data.isEquip,data.upgrade,data.sell,data.level,data.name,data.wpn,data.atk)


end

function M.isInCd(self)
    if self.lastOpTime and tick()-self.lastOpTime<0.5 then
        return  false
    end
    self.lastOpTime=tick()
    return true
end

--获取事件发送数据
function M.GetEventData(self)
    print("-------GetEventData-------",self.Data.id)
    return{self.Data.id}
end

-- 邀请某位好友
local SocialService=game:GetService("SocialService")
local Game_Cfg=_G.CommonOHOH.config.Game
local a=math.floor(Game_Cfg.FriendBoost*100)
local InvitedStr=string.format("Play with your friend to get +%d%% boost!",a)
function M.InvitedFriend(self)

    print("正在邀请好友userId:",self.Data.userId)
    self:AddProcess(function()
        local invitedOptions=Instance.new("ExperienceInviteOptions")
        invitedOptions.InviteUser=self.Data.userId
        invitedOptions.PromptMessage=InvitedStr
        SocialService:PromptGameInvite(game.Players.LocalPlayer,invitedOptions)
    end)
end

return M